﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.SERVICE.DAL;
using KPIS.GERP.SERVICE.Demo.Classes;
using KPIS.GERP.SERVICE.Model;
using Telerik.Web.UI;

namespace KPIS.GERP.SERVICE.Demo.Ekk.MySampleFlow
{
    public partial class CreateNewRequest : Page
    {
        public SsMng ssMng = new SsMng();
        public string Mode { get { return string.IsNullOrWhiteSpace(Request["mode"]) ? "vs" : Request["mode"]; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            new SsMng().CheckUss();
            
            LoadHistoryMyRequest();
            LoadHistoryWaitingMyApprove();

            if (!Page.IsPostBack)
            {
                LoadHistoryApproveModel();
                LoadApprovalPathModel();
                Page.DataBind();
            }
        }


        private void LoadHistoryMyRequest()
        {
            if (Mode.Equals("vs"))
            {
                using (var c = new ApprovalServiceWcfVsRef.AbsenceApprovalWCFClient())
                {
                    List<approval_task> tasks = c.GetTasks().ToList();
                    List<approval_task> tasksMyRequest =
                        tasks.Where(task => task.RequestEmpSeq == ssMng.EmpSeqI).ToList();
                    RadGrid1.DataSource = tasksMyRequest;
                    RadGrid1.DataBind();
                }
            }
            else
            {
                using (var c = new ApprovalServiceWcfIisRef.AbsenceApprovalWCFClient())
                {
                    List<approval_task> tasks = c.GetTasks().ToList();
                    List<approval_task> tasksMyRequest =
                        tasks.Where(task => task.RequestEmpSeq == ssMng.EmpSeqI).ToList();
                    RadGrid1.DataSource = tasksMyRequest;
                    RadGrid1.DataBind();
                }
            }
        }

        private void LoadHistoryWaitingMyApprove()
        {
            if (Mode.Equals("vs"))
            {
                using (var c = new ApprovalServiceWcfVsRef.AbsenceApprovalWCFClient())
                {
                    var tasks = c.GetTasks();
                    var tasksMyApprove = new List<approval_task>();
                    foreach (approval_task task in tasks)
                    {

                    }
                    gridRadMyWaitingApprove.DataSource = tasksMyApprove;
                    gridRadMyWaitingApprove.DataBind();
                }
            }
            else
            {
                using (var c = new ApprovalServiceWcfIisRef.AbsenceApprovalWCFClient())
                {
                    var tasks = c.GetTasks();
                    var tasksMyApprove = new List<approval_task>();
                    foreach (approval_task task in tasks)
                    {

                    }
                    gridRadMyWaitingApprove.DataSource = tasksMyApprove;
                    gridRadMyWaitingApprove.DataBind();
                }
            }
        }


        private void LoadApprovalPathModel()
        {
            RadGrid2.DataSource = null;
            RadGrid2.DataBind();
        }

        private void LoadHistoryApproveModel()
        {
            RadTreeList1.DataSource = null;
            RadTreeList1.DataBind();
            RadTreeList1.ExpandAllItems();
        }

        protected void btnRadCreateNewRequest_Click(object sender, EventArgs e)
        {
            try
            {
                string errorMessage;
                if (AbsenceRequestForm1.CheckValidate(out errorMessage))
                {
                    if (Mode.Equals("vs"))
                    {
                        using (var c = new CheckRulesServiceRef.CheckRulesServiceClient())
                        {
                            string absenceSeq;
                            lblMessageResult.Text = c.GetRequestData(ssMng.EmpSeq,
                                                                     AbsenceRequestForm1.AbsenceType,
                                                                     Convert.ToDateTime(AbsenceRequestForm1.DateStart),
                                                                     Convert.ToDateTime(AbsenceRequestForm1.DateEnd),
                                                                     String.Empty,
                                                                     AbsenceRequestForm1.Remark,
                                                                     ssMng.EmpSeq,
                                                                     AbsenceRequestForm1.TimePeriodStart,
                                                                     AbsenceRequestForm1.TimePeriodEnd,
                                                                     out absenceSeq);
                            AbsenceSeq = absenceSeq;
                            btnRadCreateNewRequest.Visible = false;
                            btnRadCreateNewRequestConfirm.Visible = true;
                            btnRadCreateNewRequestCancel.Visible = true;
                        }
                    }
                    else
                    {
                        using (var c = new CheckRulesServiceIisRef.CheckRulesServiceClient())
                        {
                            string absenceSeq;
                            lblMessageResult.Text = c.GetRequestData(ssMng.EmpSeq,
                                                                     AbsenceRequestForm1.AbsenceType,
                                                                     Convert.ToDateTime(AbsenceRequestForm1.DateStart),
                                                                     Convert.ToDateTime(AbsenceRequestForm1.DateEnd),
                                                                     String.Empty,
                                                                     AbsenceRequestForm1.Remark,
                                                                     ssMng.EmpSeq,
                                                                     AbsenceRequestForm1.TimePeriodStart,
                                                                     AbsenceRequestForm1.TimePeriodEnd,
                                                                     out absenceSeq);
                            AbsenceSeq = absenceSeq;
                            btnRadCreateNewRequest.Visible = false;
                            btnRadCreateNewRequestConfirm.Visible = true;
                            btnRadCreateNewRequestCancel.Visible = true;
                        }
                    }
                }
                else
                {
                    lblMessageResult.Text = "ข้อมูลในการลาไม่สมบูรณ์ <br/> " + errorMessage;
                }
            }
            catch (Exception er)
            {
                lblMessageResult.Text = "เกิดปัญหาขึ้นตอนเริ่ม flow check absence <br />" + er.Message;
                btnRadCreateNewRequest.Visible = false;
                btnRadCreateNewRequestConfirm.Visible = true;
            }
        }

        protected void btnRadCreateNewRequestConfirm_Click(object sender, EventArgs e)
        {
            if (Mode.Equals("vs"))
            {
                using (var c = new ApprovalServiceVsRef.ServiceClient())
                {
                    if (!string.IsNullOrWhiteSpace(AbsenceSeq))
                    {
                        string outMessage;
                        c.CreateAbsenceTask(txtRadTitle.Text, ssMng.EmpSeqI,
                                            Convert.ToInt32(AbsenceSeq),
                                            "hrm_ps_absence", "absence_seq", "absence_type_seq",DAL. Common.ProcessStatus,
                                            out outMessage);
                        lblMessageResult.Text += "<br>" + outMessage;
                    }
                    else
                    {
                        lblMessageResult.Text += "<br>" + "ไม่มีข้อมูลการลา ไม่สามารถส่งคำขออนมัติได้";
                    }
                }
            }
            else
            {
                using (var c = new ApprovalServiceIisRef.ServiceClient())
                {
                    if (!string.IsNullOrWhiteSpace(AbsenceSeq))
                    {
                        string outMessage;
                        c.CreateAbsenceTask(txtRadTitle.Text, ssMng.EmpSeqI,
                                            Convert.ToInt32(AbsenceSeq),
                                            "hrm_ps_absence", "absence_seq", "absence_type_seq", DAL.Common.ProcessStatus,
                                            out outMessage);
                        lblMessageResult.Text += "<br>" + outMessage;
                    }
                    else
                    {
                        lblMessageResult.Text += "<br>" + "ไม่มีข้อมูลการลา ไม่สามารถส่งคำขออนมัติได้";
                    }
                }
            }
            btnRadCreateNewRequestConfirm.Visible = false;
            btnRadCreateNewRequestCancel.Visible = false;
            btnRadRedirectNew.Visible = true;
        }

        protected void btnRadCreateNewRequestCancel_Click(object sender, EventArgs e)
        {
            if (Mode.Equals("vs"))
            {
                using (var c = new ApprovalServiceVsRef.ServiceClient())
                {
                    string outMessage;
                    c.CreateAbsenceTask(txtRadTitle.Text, ssMng.EmpSeqI,
                                        Convert.ToInt32(AbsenceSeq),
                                        "hrm_ps_absence", "absence_seq", "absence_type_seq", DAL.Common.DeletedStatus,
                                        out outMessage);
                    lblMessageResult.Text += "<br>" + outMessage;
                }
            }
            else
            {
                using (var c = new ApprovalServiceIisRef.ServiceClient())
                {
                    string outMessage;
                    c.CreateAbsenceTask(txtRadTitle.Text, ssMng.EmpSeqI,
                                        Convert.ToInt32(AbsenceSeq),
                                        "hrm_ps_absence", "absence_seq", "absence_type_seq", DAL.Common.DeletedStatus,
                                        out outMessage);
                    lblMessageResult.Text += "<br>" + outMessage;
                }
            }
            btnRadCreateNewRequestConfirm.Visible = false;
            btnRadCreateNewRequestCancel.Visible = false;
        }


        public string AbsenceSeq
        {
            get { return lblAbsenceSeq.Text; }
            set { lblAbsenceSeq.Text = value; }
        }

        public string InitilialTaskTitle
        {
            get { return String.Format("คำขอ{0} ของ {1}", RadComboBox1.Text, ssMng.UserFullName); }
        }

        protected void btnRadRedirectNew_Click(object sender, EventArgs e)
        {
            RedirectMode(Mode);
        }

        public void RedirectMode(string mode)
        {
            if (mode == null)
                mode = "vs";

            HttpContext.Current.Response.Redirect("CreateNewRequest.aspx?mode=" + mode);
        }

        public void LinkButton_Command(Object sender, CommandEventArgs e)
        {
            // Cancel Command
            var wfInstantId = new Guid(Convert.ToString(e.CommandArgument));
            
            using (var sc = new ApprovalServiceVsRef.ServiceClient())
            {
                sc.ApproveAbsence(wfInstantId, TaskAction.C);
            }
            
        }
    }
}